package com.wgts.wgts.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wgts.wgts.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class MoHuGlide {

    private MoHuGlide(){}

    private static class SingletonHolder{
        private static final MoHuGlide _INSTANCE = new MoHuGlide();
    }

    public static MoHuGlide getInstance(){
        return SingletonHolder._INSTANCE;
    }

    /**
     * 加载列表图片防止错乱
     * @param imageView
     * @param uri
     * @param context
     */
    public void glideImgsFormList(ImageView imageView, String uri, Context context){
        RequestOptions requestOptions = new RequestOptions().placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);


        imageView.setTag(R.id.imageid,uri);
        if(imageView.getTag(R.id.imageid) != null && uri == imageView.getTag(R.id.imageid)){
            Glide.with(context).load(uri).apply(requestOptions).into(imageView);
        }
    }

    /**
     * 列表加载圆角
     * @param imageView
     * @param uri
     * @param context
     */
    public void glideImgsFormListRaduice(ImageView imageView, String uri, Context context){
        RequestOptions requestOptions = new RequestOptions().transform(new RoundedCornersTransformation(10, 0)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);


        imageView.setTag(R.id.imageid,uri);
        if(imageView.getTag(R.id.imageid) != null && uri == imageView.getTag(R.id.imageid)){
            Glide.with(context).load(uri).apply(requestOptions).into(imageView);
        }
    }


    /**
     * top
     * @param context
     * @param url
     * @param imageView
     */
    public void topFormListRaduice(Context context, String  url, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().bitmapTransform(new RoundedCornersTransformation(10, 0, RoundedCornersTransformation.CornerType.TOP)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }



    public void glideImg(Context context, String url, ImageView imageView){

        RequestOptions requestOptions = new RequestOptions().placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);



        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }

    public void clearMemory(Context context){
        //清空内存缓存，要求在主线程中执行
        Glide.get(context).clearMemory();

        //清空磁盘缓存，要求在后台线程中执行
        Glide.get(context).clearDiskCache();
    }

    /*
    * 根据网络图片来判断展示的图片宽高
    * */

    public void MatchImage(final Context context, String url, final ImageView imgView){
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new BitmapImageViewTarget(imgView) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int width = resource.getWidth();
                        int height = resource.getHeight();
                        CustomImageView.getInstance().customView(context, imgView, height/(double)width);
                        imgView.setImageBitmap(resource);
                    }
                });

    }

    /**
     * 圆角
     * 全部圆角
     * @param context
     * @param resourceId
     * @param imageView
     */
    public void raduice(Context context, Integer resourceId, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().transform(new RoundedCornersTransformation(10, 0)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);

        Glide.with(context)
                .load(resourceId)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     * 圆角
     * 全部圆角
     * @param context
     * @param url
     * @param imageView
     */
    public void raduice(Context context, String url, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().transform(new RoundedCornersTransformation(10, 0)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);

    }


    public void raduice(Context context, String url, int resource, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().transform(new RoundedCornersTransformation(10, 0)).placeholder(resource) //设置“加载中”状态时显示的图片
                .error(resource);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);

    }

    /**
     *
     *上部圆角
     * @param context
     * @param url
     * @param imageView
     */
    public void topRaduice(Context context, String  url, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().bitmapTransform(new RoundedCornersTransformation(10, 0, RoundedCornersTransformation.CornerType.TOP)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }


    /**
     *
     *上部圆角
     * @param context
     * @param resourceId
     * @param imageView
     */
    public void topRaduice(Context context, int  resourceId, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().bitmapTransform(new RoundedCornersTransformation(10, 0, RoundedCornersTransformation.CornerType.TOP)).placeholder(R.mipmap.bitmap_white) //设置“加载中”状态时显示的图片
                .error(R.mipmap.bitmap_grey);

        Glide.with(context)
                .load(resourceId)
                .apply(requestOptions)
                .into(imageView);
    }


    public void loadGif(Context context, String  url, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher);
        Glide.with(context).load(url).apply(requestOptions).into(imageView);
    }


    /**
     *加载ICON， 加载中显示APP内图片，加载错误显示App图片、
     * @param context
     * @param url
     * @param imageView
     * @param resourceId
     */
    public void loadIconImg(Context context, String  url, ImageView imageView, int resourceId){
        RequestOptions requestOptions = new RequestOptions().placeholder(resourceId).error(resourceId); //设置“加载中”状态时显示的图片



        Glide.with(context).load(url).apply(requestOptions).into(imageView);
    }

    public Bitmap loadBitmap(Context context, String url) throws ExecutionException, InterruptedException {
       // Glide.with(context).asBitmap().load(url).
        return Glide.with(context)
                .asBitmap()
                .load(url)
                .submit().get();
    }


    public static  Bitmap returnBitmap(String url) {
        URL fileUrl = null;
        Bitmap bitmap = null;

        try {
            fileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) fileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }









}
